package org.mzy.pet.service.impl;

import org.mzy.basic.config.BussinessException;
import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.basic.util.BaiduAuditUtils;
import org.mzy.pet.domain.Pet;
import org.mzy.pet.domain.PetDetail;
import org.mzy.pet.domain.PetOnlineAuditLog;
import org.mzy.pet.mapper.PetDetailMapper;
import org.mzy.pet.mapper.PetMapper;
import org.mzy.pet.mapper.PetOnlineAuditLogMapper;
import org.mzy.pet.service.IPetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void add(Pet pet) {
        // 保存宠物信息
        petMapper.add(pet);
        // 如果详情不为空，就添加详情信息把这个宠物id赋值给详情的宠物id，保存详情信息
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    /*上架*/
    @Override
    public void onsale(List<Long> ids) throws Exception {
        // 遍历id集合,获取pet数据
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            // new一个String来存数据
            StringBuffer stringBuffer = new StringBuffer();
            // 如果审核名字不合规，则
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                stringBuffer.append("宠物名称不合规" + pet.getName() + ";");
            }
            // 如果图片为空，就抛异常
            if (pet.getResources() == null) {
                throw new BussinessException("图片不合规");
            }
            // 如果有两张图片，则按逗号分开
            if (pet.getResources().split(",").length > 1) {
                if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])) {
                    stringBuffer.append("宠物图片不合规" + pet.getResources() + ";");
                }
            } else {
                // 如果审核图片不合规，也抛异常
                if (!BaiduAuditUtils.ImgCensor(pet.getResources())) {
                    stringBuffer.append("宠物图片不合规" + pet.getResources() + ";");
                }
            }
            // 如果stringBuffer的长度大于1，说明有内容不合规，就跳过此数据
            if (stringBuffer.length() > 0) {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                // 保存日志状态为失败
                log.setState(0);
                // 日志文本为异常文本
                log.setNote(stringBuffer.toString());
                log.setAuditTime(new Date());
                petOnlineAuditLogMapper.add(log);

                continue;
            }
            petMapper.onsale(id);
//            FreemarkUtil.getHtml(id);
        }
    }

    /*下架*/
    @Override
    public void offsale(List<Long> ids) {
        // 调用下架方法
        petMapper.offsale(ids);
    }

    /*获取宠物详情*/
    @Override
    public Pet getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }

    /*删除*/
    @Override
    public void delete(Long id) {
        petMapper.delete(id);
        petDetailMapper.deleteByPetId(id);
    }

    /*批量删除*/
    @Override
    public void batchDel(List<Long> ids) {
        petMapper.batchDel(ids);
        petDetailMapper.batchDelByPetId(ids);
    }

    /*修改*/
    @Override
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.updateByPetId(petDetail);
    }
}
